#Region " Imports "

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports BA.Attachments
Imports BA.Images
Imports BlazeApps.Library.Resources
Imports BlazeApps.Library.TreeViews

#End Region

Public Class WebBase
    Inherits Security

#Region " Enums "

    Protected Enum UploadType
        NewItem = 0
        UpdateItem = 1
    End Enum

    Protected Enum MessageListType
        Inbox = 0
        SentItems = 1
    End Enum

    Protected Enum MessageViewIndex
        'SendMessage = 0
		ListItems = 0
		Message = 1
    End Enum

#End Region

#Region " Members "

    Dim PU As New ProfileUtility

#End Region

#Region " Properties "

    Public Shared ReadOnly Property AppPage() As String
        Get
            Dim CurrentPage As String = HttpContext.Current.Request.ServerVariables("PATH_INFO")

            Return Mid(CurrentPage, (InStrRev(CurrentPage, "\") + 1), (InStrRev(CurrentPage, ".") - (InStrRev(CurrentPage, "\") + 1)))

        End Get
    End Property

    Public Shared ReadOnly Property AppPath() As String
        Get
            Dim path As String = HttpContext.Current.Request.ApplicationPath
            If Not path.EndsWith("/") Then
                path += "/"
            End If
            Return path
        End Get
    End Property

    Public Shared ReadOnly Property PhysicalAppPath() As String
        Get
            Dim path As String = HttpContext.Current.Request.PhysicalApplicationPath
            If Not path.EndsWith("\") Then
                path += "\"
            End If
            Return path
        End Get
    End Property

    Public Shared ReadOnly Property AppTheme() As String
        Get
            Dim Config As New WebConfigSettings

            If Not Config.ConfigTheme = String.Empty Then
                Return Config.ConfigTheme
            Else
                Return "Default"
            End If
        End Get
    End Property

    Protected ReadOnly Property DropDownDefaultText() As String
        Get
            Return "[ Select ]"
        End Get
    End Property

    Protected ReadOnly Property DropDownDefaultValue() As String
        Get
            Return "-1"
        End Get
    End Property

    Protected ReadOnly Property DropDownDefaultGuidValue() As String
        Get
            Return "00000000-0000-0000-0000-000000000000"
        End Get
    End Property

    Public Shared ReadOnly Property AppInstalled() As Boolean
        Get
            Return CType(ConfigurationManager.AppSettings("AppInstalled"), Boolean)
        End Get
    End Property

    Public ReadOnly Property ItemsPerPage() As Integer
        Get
            If IsUserAuthenticated Then
                Return PU.GetItemPerPage(CurrentUser)
            Else
                Return 10
            End If
        End Get
    End Property

    Public ReadOnly Property HighLightSearch() As Boolean
        Get
            If IsUserAuthenticated Then
                Return PU.HighLightSearch(CurrentUser)
            Else
                Return False
            End If
        End Get
    End Property

    Public ReadOnly Property HighLightExactMatch() As Boolean
        Get
            If IsUserAuthenticated Then
                Return PU.HighLightExactMatch(CurrentUser)
            Else
                Return False
            End If
        End Get
    End Property

#End Region

#Region " Cookie Methods "

    Protected Function GetGuidFromQueryCookie(ByVal ItemValue As String) As Guid

        If IsNothing(Request.Cookies(ItemValue)) OrElse Request.Cookies(ItemValue).ToString() = String.Empty Then
            Return Guid.Empty
        Else
            Return New Guid(Request.Cookies(ItemValue).ToString)
        End If

    End Function

    Protected Function GetStringFromQueryCookie(ByVal ItemValue As String) As String

        If IsNothing(Request.Cookies(ItemValue)) OrElse Request.Cookies(ItemValue).ToString() = String.Empty Then
            Return String.Empty
        Else
            Return New String(Request.Cookies(ItemValue).ToString)
        End If

    End Function

    Protected Function GetIntegerFromQueryCookie(ByVal ItemValue As String) As Integer

        If IsNothing(Request.Cookies(ItemValue)) OrElse Request.Cookies(ItemValue).ToString() = String.Empty Then
            Return 0
        Else
            Return CType(Request.Cookies(ItemValue).ToString, Integer)
        End If

    End Function

    Protected Function GetBooleanFromQueryCookie(ByVal ItemValue As String) As Boolean

        If IsNothing(Request.Cookies(ItemValue)) OrElse Request.Cookies(ItemValue).ToString() = String.Empty Then
            Return False
        Else
            If IsNumeric(Request.Cookies(ItemValue)) Then
                If Request.Cookies(ItemValue).Equals("1") Then
                    Return True
                Else
                    Return False
                End If
            Else
                If Request.Cookies(ItemValue).ToString.ToLower.Equals("true") Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If

    End Function

#End Region

#Region " QueryString Methods "

	Public Function GetResourceTypeFromQueryString(ByVal ItemValue As String) As TreeViewNodes.TreeType

		If IsNothing(Request.QueryString(ItemValue)) OrElse Request.QueryString(ItemValue).ToString() = String.Empty Then
			Return TreeViewNodes.TreeType.ResourceTree
		Else
			Return CType(System.Enum.Parse(GetType(TreeViewNodes.TreeType), Request.QueryString(ItemValue), True), TreeViewNodes.TreeType)
		End If

	End Function

	Public Function GetGuidFromQueryString(ByVal ItemValue As String) As Guid

		If IsNothing(Request.QueryString(ItemValue)) OrElse Request.QueryString(ItemValue).ToString() = String.Empty Then
			Return Guid.Empty
		Else
			Return New Guid(Request.QueryString(ItemValue))
		End If

	End Function

	Public Function GetStringFromQueryString(ByVal ItemValue As String) As String

		If String.IsNullOrEmpty(Request.QueryString(ItemValue.ToString())) Then
			Return String.Empty
		Else
			Return New String(Request.QueryString(ItemValue))
		End If

	End Function

	Public Function GetIntegerFromQueryString(ByVal ItemValue As String) As Integer

		If IsNothing(Request.QueryString(ItemValue)) OrElse Request.QueryString(ItemValue).ToString() = String.Empty Then
			Return 0
		Else
			Return CType(Request.QueryString(ItemValue), Integer)
		End If

	End Function

	Public Function GetBooleanFromQueryString(ByVal ItemValue As String) As Boolean

		If String.IsNullOrEmpty(Request.QueryString(ItemValue)) Then
			Return False
		Else
			If IsNothing(Request.QueryString(ItemValue)) OrElse Request.QueryString(ItemValue).ToString() = String.Empty Then
				Return False
			Else
				If IsNumeric(Request.QueryString(ItemValue)) Then
					If Request.QueryString(ItemValue).Equals("1") Then
						Return True
					Else
						Return False
					End If
				Else
					If Request.QueryString(ItemValue).ToString.ToLower.Equals("true") Then
						Return True
					Else
						Return False
					End If
				End If
			End If
		End If

	End Function

#End Region

#Region " Session Methods "

    Protected Function GetGuidFromSession(ByVal ItemValue As String) As Guid

        If IsNothing(Session(ItemValue)) OrElse Session(ItemValue).ToString() = String.Empty Then
            Return Guid.Empty
        Else
            Return New Guid(Session(ItemValue).ToString)
        End If

    End Function

    Protected Function GetStringFromSession(ByVal ItemValue As String) As String

        If IsNothing(Session(ItemValue)) OrElse Session(ItemValue).ToString() = String.Empty Then
            Return String.Empty
        Else
            Return New String(Session(ItemValue))
        End If

    End Function

    Protected Function GetIntegerFromSession(ByVal ItemValue As String) As Integer

        If IsNothing(Session(ItemValue)) OrElse Session(ItemValue).ToString() = String.Empty Then
            Return 0
        Else
            Return CType(Session(ItemValue), Integer)
        End If

    End Function

    Protected Function GetBooleanFromSession(ByVal ItemValue As String) As Boolean

        If IsNothing(Session(ItemValue)) OrElse Session(ItemValue).ToString() = String.Empty Then
            Return False
        Else
            If IsNumeric(Session(ItemValue)) Then
                If Session(ItemValue).Equals("1") Then
                    Return True
                Else
                    Return False
                End If
            Else
                If Session(ItemValue).ToString.ToLower.Equals("true") Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If

    End Function

#End Region

#Region " ViewState Methods "

	Public Function GetResourceTypeFromVS(ByVal ItemValue As String) As TreeViewNodes.TreeType

		If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
			Return TreeViewNodes.TreeType.NotSet
		Else
			Return CType(System.Enum.Parse(GetType(TreeViewNodes.TreeType), ViewState(ItemValue), True), TreeViewNodes.TreeType)
		End If

	End Function

    Public Function GetGuidFromVS(ByVal ItemValue As String) As Guid

        If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
            Return Guid.Empty
        Else
            Return New Guid(ViewState(ItemValue).ToString())
        End If

    End Function

    Public Function GetStringFromVS(ByVal ItemValue As String) As String

        If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
            Return String.Empty
        Else
            Return New String(ViewState(ItemValue))
        End If

    End Function

    Public Function GetLongIntegerFromVS(ByVal ItemValue As String) As Int64

        If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
            Return 0
        Else
            Return CType(ViewState(ItemValue), Int64)
        End If

    End Function

    Public Function GetIntegerFromVS(ByVal ItemValue As String) As Integer

        If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
            Return 0
        Else
            Return CType(ViewState(ItemValue), Integer)
        End If

    End Function

    Protected Function GetBooleanFromVS(ByVal ItemValue As String) As Boolean
        If IsNothing(ViewState(ItemValue)) OrElse ViewState(ItemValue).ToString() = String.Empty Then
            Return False
        Else
            If IsNumeric(ViewState(ItemValue)) Then
                If ViewState(ItemValue).Equals("1") OrElse ViewState(ItemValue).ToString.ToLower.Equals("true") Then
                    Return True
                Else
                    Return False
                End If
            Else
                If ViewState(ItemValue).ToString.ToLower.Equals("true") Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If
    End Function

#End Region

#Region " General Methods "

    Protected Function ConvertToGuid(ByVal ObjectItem As Object) As Guid

        Try
            If ObjectItem.GetType() Is GetType(TextBox) Then
                Return New Guid(CType(ObjectItem, TextBox).Text.ToString.Trim)
            ElseIf ObjectItem.GetType() Is GetType(DropDownList) Then
                Return New Guid(CType(ObjectItem, DropDownList).SelectedValue)
            ElseIf ObjectItem.GetType() Is GetType(RadioButtonList) Then
                Return New Guid(CType(ObjectItem, RadioButtonList).SelectedValue)
            ElseIf ObjectItem.GetType() Is GetType(Label) Then
                Return New Guid(CType(ObjectItem, Label).Text.ToString.Trim)
            ElseIf ObjectItem.GetType() Is GetType(TreeView) Then
                Return New Guid(CType(ObjectItem, TreeView).SelectedValue)
            Else
                Return New Guid(ObjectItem.ToString.Trim)
            End If
        Catch ex As Exception
            Return Guid.Empty
        End Try

    End Function

#End Region

#Region " Meta Tags "

    Protected Sub AddMetaTags(ByVal Keywords As String, ByVal Description As String)
        Dim MetaKeywords As New HtmlMeta
        Dim MetaDescription As New HtmlMeta

        MetaKeywords.Name = "keywords"
        MetaKeywords.Content = Keywords

        MetaDescription.Name = "description"
        MetaDescription.Content = Description

        Me.Page.Header.Controls.Add(MetaKeywords)
        Me.Page.Header.Controls.Add(MetaDescription)

    End Sub

#End Region

#Region " Find Controls "

    Public Shared Function FindAnyControl(ByVal Item As Control, ByVal ControlName As String) As Control
        If Item.ID = ControlName Then
            Return Item
        End If
        For Each Ctl As Control In Item.Controls
            Dim FoundCtl As Control = FindAnyControl(Ctl, ControlName)
            If Not (FoundCtl Is Nothing) Then
                Return FoundCtl
            End If
        Next
        Return Nothing
    End Function

#End Region

End Class